package user.utils;

import java.util.Scanner;

/** 
 * Class prints list of palindromes from the input string
 *
 * @version      
         1.02
 * @author          
         Alex Kebkalo  
 */
public class Palindromes {
	public static void main(String[] args) {
        String inputstring;
        int length;
        boolean palindromeFound = false;

        System.out.print("Type string:");
        Scanner scan = new Scanner(System.in);
		inputstring = scan.nextLine();
        scan.close();
        length = inputstring.length();
		System.out.println("Palindromes list:");
		for (int currentPosition = 1; currentPosition < length - 1; currentPosition++) {

            //checking palindromes with even and odd length
			if (PrintPalindromes(inputstring, currentPosition, currentPosition + 1)
					|| PrintPalindromes(inputstring, currentPosition - 1, currentPosition + 1)) {
				palindromeFound = true;
			}
		}
		if (!palindromeFound)
			System.out.println("Found 0 palindromes.");
	}

	/**
	 * Method checks if there is palindrome in inputstring
	 * at position from palindromestart to palindromeend and prints it.
	 * Method returns true if palindrome was found and false otherwise.
	 */
	static boolean PrintPalindromes(String inputString, int palindromeStart, int palindromeEnd) {
		final int MIN_PALINDROME_LENGTH = 2;
		boolean palindromeFound = false;

		while (equalSymbols(inputString, palindromeStart, palindromeEnd)) {
			if (palindromeEnd - palindromeStart >= MIN_PALINDROME_LENGTH) {
				System.out.println(inputString.substring(palindromeStart, 1 + palindromeEnd));
				palindromeFound = true;
			}
			palindromeStart--;
			palindromeEnd++;
		}
		return palindromeFound;
	}

    static boolean inRange (int start, int end, int length){
        return (start >= 0) && (end < length);
    }

    static boolean equalSymbols (String inputString, int position1, int position2){
        if (!inRange(position1, position2, inputString.length())) {
            return false;
        } else {
            return inputString.codePointAt(position1) == inputString.codePointAt(position2);
        }
    }
}